﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Dynamic;
using System.Text;
using System.Threading.Tasks;
using Abp;
using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.AutoMapper;
using Abp.Configuration;
using Abp.Domain.Repositories;
using Abp.Extensions;
using Abp.Linq.Extensions;
using LeadThing.AbpZeroTemplate.VersionApps.Authorization;
using LeadThing.AbpZeroTemplate.VersionApps.Dtos;
using LeadThing.AbpZeroTemplate.Dto;

namespace LeadThing.AbpZeroTemplate.VersionApps
{
    /// <summary>
    /// APP版本号服务实现
    /// </summary>


    public class VersionAppAppService : AbpZeroTemplateAppServiceBase, IVersionAppAppService
    {
        private readonly IRepository<VersionApp, long> _versionAppRepository;
        private readonly IVersionAppListExcelExporter _versionAppListExcelExporter;


        private readonly VersionAppManage _versionAppManage;
        /// <summary>
        /// 构造方法
        /// </summary>
        public VersionAppAppService(IRepository<VersionApp, long> versionAppRepository,
VersionAppManage versionAppManage
      , IVersionAppListExcelExporter versionAppListExcelExporter
  )
        {
            _versionAppRepository = versionAppRepository;
            _versionAppManage = versionAppManage;
            _versionAppListExcelExporter = versionAppListExcelExporter;
        }


        #region 实体的自定义扩展方法
        private IQueryable<VersionApp> _versionAppRepositoryAsNoTrack => _versionAppRepository.GetAll().AsNoTracking();

        /// <summary>
        /// 获取所有APP标记
        /// </summary>
        /// <returns></returns>
        public List<ComboboxItemDto> GetAllVersionFlagList()
        {
            return _versionAppManage.GetAllVersionFlagList();
        }

        #endregion

        #region APP版本号管理

        /// <summary>
        /// 根据查询条件获取APP版本号分页列表
        /// </summary>
        public async Task<PagedResultDto<VersionAppListDto>> GetPagedVersionAppsAsync(GetVersionAppInput input)
        {

            var query = _versionAppRepositoryAsNoTrack;
            //TODO:根据传入的参数添加过滤条件

            if (!string.IsNullOrWhiteSpace(input.Filter))
            {
                query = query.Where(p => p.Desc.Contains(input.Filter));
            }

            var versionAppCount = await query.CountAsync();

            var versionApps = await query
            .OrderBy(input.Sorting)
            .PageBy(input)
            .ToListAsync();

            var flagList = GetAllVersionFlagList();

            var versionAppListDtos = versionApps.MapTo<List<VersionAppListDto>>();
            return new PagedResultDto<VersionAppListDto>(
            versionAppCount,
            versionApps.Select(item =>
            {
                var dto = item.MapTo<VersionAppListDto>();

                var flagModel = flagList.Where(p => p.Value == dto.Flag).FirstOrDefault();
                if (flagModel != null)
                {
                    dto.FlagName = flagModel.DisplayText;
                }

                return dto;
            }).ToList());
        }

        /// <summary>
        /// 通过Id获取APP版本号信息进行编辑或修改 
        /// </summary>
        public async Task<GetVersionAppForEditOutput> GetVersionAppForEditAsync(NullableIdDto<long> input)
        {
            var output = new GetVersionAppForEditOutput();

            VersionAppEditDto versionAppEditDto;

            if (input.Id.HasValue)
            {
                var entity = await _versionAppRepository.GetAsync(input.Id.Value);
                versionAppEditDto = entity.MapTo<VersionAppEditDto>();
            }
            else
            {
                versionAppEditDto = new VersionAppEditDto();
            }

            output.VersionApp = versionAppEditDto;
            return output;
        }

        /// <summary>
        /// 通过Id获取APP版本号信息进行编辑或修改 
        /// </summary>
        public async Task<GetVersionAppForEditOutput> GetVersionAppForEditAsync(EntityDto<string> input)
        {
            var output = new GetVersionAppForEditOutput();

            VersionAppEditDto versionAppEditDto;

            if (!string.IsNullOrWhiteSpace(input.Id))
            {
                var id = Convert.ToInt32(input.Id);
                var entity = await _versionAppRepository.GetAsync(id);
                versionAppEditDto = entity.MapTo<VersionAppEditDto>();
            }
            else
            {
                versionAppEditDto = new VersionAppEditDto();
            }

            output.VersionApp = versionAppEditDto;
            return output;
        }


        /// <summary>
        /// 通过指定id获取APP版本号ListDto信息
        /// </summary>
        public async Task<VersionAppListDto> GetVersionAppByIdAsync(EntityDto<long> input)
        {
            var entity = await _versionAppRepository.GetAsync(input.Id);

            return entity.MapTo<VersionAppListDto>();
        }


        /// <summary>
        /// 新增或更改APP版本号
        /// </summary>
        public async Task CreateOrUpdateVersionAppAsync(CreateOrUpdateVersionAppInput input)
        {
            if (input.VersionAppEditDto.Id.HasValue)
            {
                await UpdateVersionAppAsync(input.VersionAppEditDto);
            }
            else
            {
                await CreateVersionAppAsync(input.VersionAppEditDto);
            }
        }


        /// <summary>
        /// 新增APP版本号
        /// </summary>
        //[AbpAuthorize(VersionAppAppPermissions.VersionApp_CreateVersionApp)]
        public virtual async Task<VersionAppEditDto> CreateVersionAppAsync(VersionAppEditDto input)
        {
            //TODO:新增前的逻辑判断，是否允许新增

            var entity = input.MapTo<VersionApp>();

            entity = await _versionAppRepository.InsertAsync(entity);
            return entity.MapTo<VersionAppEditDto>();
        }

        /// <summary>
        /// 编辑APP版本号
        /// </summary>
        //[AbpAuthorize(VersionAppAppPermissions.VersionApp_EditVersionApp)]
        public virtual async Task UpdateVersionAppAsync(VersionAppEditDto input)
        {
            //TODO:更新前的逻辑判断，是否允许更新

            var entity = await _versionAppRepository.GetAsync(input.Id.Value);
            input.MapTo(entity);

            await _versionAppRepository.UpdateAsync(entity);
        }

        /// <summary>
        /// 删除APP版本号
        /// </summary>
        [AbpAuthorize(VersionAppAppPermissions.VersionApp_DeleteVersionApp)]
        public async Task DeleteVersionAppAsync(EntityDto<long> input)
        {
            //TODO:删除前的逻辑判断，是否允许删除
            await _versionAppRepository.DeleteAsync(input.Id);
        }

        /// <summary>
        /// 批量删除APP版本号
        /// </summary>
        [AbpAuthorize(VersionAppAppPermissions.VersionApp_DeleteVersionApp)]
        public async Task BatchDeleteVersionAppAsync(List<long> input)
        {
            //TODO:批量删除前的逻辑判断，是否允许删除
            await _versionAppRepository.DeleteAsync(s => input.Contains(s.Id));
        }

        #endregion

        #region APP版本号的Excel导出功能


        public async Task<FileDto> GetVersionAppToExcel()
        {
            var entities = await _versionAppRepository.GetAll().ToListAsync();

            var dtos = entities.MapTo<List<VersionAppListDto>>();

            var fileDto = _versionAppListExcelExporter.ExportVersionAppToFile(dtos);



            return fileDto;
        }


        #endregion

    }
}
